﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Module/ApplyView/_Layout.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Thksoft.Module.ApplyView.Apply.Edit.Index" %>
<asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolder" runat="server">

<style type="text/css">
    .Apply-Row-Lev-1 {
        background-color: #B2D8FF;
        font-size: 14px;
    }

    .Apply-Row-Lev-2 {
        background-color: #CCCCCC;
        font-size: 13px;
    }

    .Apply-Row-Lev-3 {
        background-color: white;
        font-size: 12px;
    }

    .Apply-Row-Lev-1 .INPUT {
        background-color: #B2D8FF;
        border-color: white;
    }

    .Apply-Row-Lev-2 .INPUT {
        background-color: #CCCCCC;
        border-color: white;
    }

    .Apply-Row-Lev-3 .INPUT {
        border-color: white;
    }
</style>
<body>
    <form id="form1" runat="server">
<%
    //通过Key获取用户名
    var userBll = new BLL.Sys_UserBLL();
    var userId = Request.Params.Get("UserId");
    var user = userBll.Find(Convert.ToInt32(userId));
    var userName = user.USER_NAME;

     %>

<div id="app" class="Content" style="top:0px;">
    <div class="Header lbltitle">
        <%=userName + "年资金计划"%>
    </div>
    <div class="Content">
        <div class="Content" style="overflow-x: auto;overflow-y: auto;top:0px;bottom:40px;">
            <table class="table" width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr style="background-color:#29a1f7;opacity:0.7; font-size:16px;">
                    <th style="border: 1px solid #BAD4E1;text-align:center;color:white;">编号</th>
                    <th style="border: 1px solid #BAD4E1;text-align:center;color:white;">项目名称</th>
                    <th style="border: 1px solid #BAD4E1;text-align:center;color:white;">申请金额</th>
                    <th style="border: 1px solid #BAD4E1;text-align:center;color:white;">备注</th>
                </tr>
                <template v-for="item in Models">
                    <tr :class="GetLev2(item)">
                        <td style="text-align:center;">{{item.N_PROJECT_ID}}</td>
                        <td>{{item.C_PROJECT_NAME}}</td>
                        <template v-if="item.N_TYPE == 2 || item.N_TYPE == 3">
                            <td style="padding-right:8px;"><input class="INPUT" style="text-align:right; width:85px;margin:0;padding:0;" :value="item.N_MONEY" @change="MoneyChange($event,item);" type="text" /></td>
                            <td style="padding-right:8px;"><input class="INPUT" style="width:100%;margin:0;padding:0;" v-model="item.C_REMARK" type="text" /></td>
                        </template>
                        <template v-else>

                            <td style="padding-right:8px;text-align:right;">{{item.N_MONEY}}</td>

                            <td style="padding-right:8px;"><input class="INPUT" style="width:100%;margin:0;padding:0;border:0px;" v-model="item.C_REMARK" type="text" /></td>
                        </template>
                    </tr>
                </template>
            </table>
        </div>
        <div class="Header" style="height:40px;bottom:0px;top:initial;text-align:center;">
            <button type="button" class="bt_css_s" @click="Save">保存</button>
        </div>
    </div>
</div>

    <script src="/Content/lib/jslinq/linq.js" type="text/javascript"></script>
    <script src="/Content/lib/vue/vue-2.5.17.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        //构造Web程序入口
        var app = new Vue({
            el: "#app",
            data: {
                Models: [],
                ApplyId:<%=Request.Params.Get("ApplyId") %>,
                UserId:<%=Request.Params.Get("UserId") %>
            },
            methods: {
                Close: function () {
                    if (top.layer == undefined) {
                        window.close();
                        return;
                    }
                    var index = top.layer.getFrameIndex(window.name); //获取窗口索引
                    try {
                        top.$("#layui-layer" + index + " .layui-layer-close").click();
                    } catch (e) {

                        console.log("已强制关闭页面!");
                        //强制关闭页面
                        try {
                            top.layer.close(index);
                        } catch (e) {
                            console.log("强制关闭页面异常!");
                        }
                    };
                },
                Save: function () {
                    var that = this;
                    var lodding = top.layer.load(1, {
                        shade: [0.8, '#393D49'],
                        content: '等待服务器端响应...',
                        success: function (layero) {
                            layero.find('.layui-layer-content').css({
                                'padding-top': '39px',
                                'width': '120px'
                            });
                        }
                    });
                    //获取信息
                    $.ajax({
                        type: "POST",
                        url: "/Module/ApplyView/ApplyHandler.ashx?MethodType=Save",
                        data: JSON.stringify({
                            Type: "YearDepartment",
                            ApplyId: that.ApplyId,
                            UserId: that.UserId,
                            Data: that.Models
                        }),
                        success: function (result) {
                            if (result.Type == 1) {
                                if (confirm("提交成功，是否关闭当前页面")) {
                                    that.Close();
                                }
                            }
                            top.layer.close(lodding);
                        },
                        error: function () {
                            alert("网络断开或服务异常");
                            top.layer.close(lodding);
                        }
                    });
                },
                GetLev: function (item) {
                    var lev = 1;
                    var that = this;
                    _GetLev(item);
                    switch (lev % 3) {
                        case 0:
                            return "Apply-Row-Lev-3";
                        case 1:
                            return "Apply-Row-Lev-1";
                        case 2:
                            return "Apply-Row-Lev-2";
                        default:
                            return "";
                    }

                    /**
                     * 递归获取等级
                     * @@param item 当前对象
                     * @@returns void
                     */
                    function _GetLev(item) {
                        if (item.N_PARENT_PROJECT_ID == 0 || item.N_PARENT_PROJECT_ID == null) {
                            return;
                        }
                        item = _GetByPid(item);
                        if (item == null) {
                            return;
                        }
                        lev += 1;
                        _GetLev(item);
                    }
                    /**
                     * 通过父级Id获取父级对象
                     * @@param item 当前对象
                     * @@returns Object 父级对象
                     */
                    function _GetByPid(item) {
                        for (var i = 0; i < that.Models.length; i++) {
                            if (that.Models[i].N_PROJECT_ID == item.N_PARENT_PROJECT_ID) {
                                return that.Models[i];
                            }
                        }
                        return null;
                    }
                },
                GetLev2: function (item) {
                    if (item.N_PARENT_PROJECT_ID == 0 || item.N_PARENT_PROJECT_ID == null) {
                        return "Apply-Row-Lev-1";
                    }
                    if (item.N_TYPE == 1) {
                        return "Apply-Row-Lev-2";
                    }
                    return "Apply-Row-Lev-3";
                },
                MoneyChange: function (event, item) {
                    var that = this;
                    if (item.N_TYPE == 2 || item.N_TYPE == 3) {
                        var value = null;
                        try {
                            if (event.target.value == "") {
                                //return;
                                event.target.value = 0;
                            }
                            value = eval(event.target.value);
                            console.log(value);
                            //value = parseFloat(event.target.value);
                        } catch (e) {
                            value = null;
                        }
                        if (value == null || value == undefined || isNaN(value) || parseFloat(value) >= 2147483647) {
                            if (confirm("请输入正确的金额,是否恢复原有数值？")) {

                                event.target.value = item.N_MONEY.toString();
                            }
                            return;
                        }
                        item.N_MONEY = parseFloat(value.toFixed(4));
                        //console.log(value);
                        var money = UpdateMoney(null);
                        console.log("总额：" + money);
                        /**
                         * 递归更新父级对象
                         * @@param pid 父级Id
                         * @@returns 金额总额
                         */
                        function UpdateMoney(pid) {
                            var arry = GetByPid(pid);
                            for (var i = 0; i < arry.length; i++) {
                                //console.log(arry);
                                if (arry[i].N_TYPE != 2 && arry[i].N_TYPE != 3) {
                                    arry[i].N_MONEY = UpdateMoney(arry[i].N_PROJECT_ID);
                                }
                            }
                            var money = 0;
                            for (var i = 0; i < arry.length; i++) {
                                if (arry[i].N_MONEY == null || arry[i].N_TYPE == 3) {
                                    continue;
                                }
                                var value = arry[i].N_MONEY;
                                //console.log(value);
                                money += value;
                            }
                            return parseFloat(money.toFixed(4));
                        }
                        /**
                         * 通过Pid 获取页面模型的对象
                         * @@param pid 父级Id
                         * @@ Arry 子级对象集合
                         */
                        function GetByPid(pid) {
                            var arry = [];
                            for (var i = 0; i < that.Models.length; i++) {
                                if (that.Models[i].N_PARENT_PROJECT_ID == pid) {
                                    arry.push(that.Models[i]);
                                }
                            }
                            return arry;
                        }
                    }
                    //重新计算Type
                }
            },
            mounted: function () {
                var that = this;

                console.log(that.ApplyId);
                console.log(that.UserId);

                //获取信息
                $.ajax({
                    async: false,
                    type: "POST",
                    url: "/Module/ApplyView/ApplyHandler.ashx?MethodType=Get",
                    data: JSON.stringify({
                        Type: "YearDepartment",
                        ApplyId: that.ApplyId,
                        UserId: that.UserId
                    }),
                    success: function (result) {
                        if (result.Type == 1) {
                            console.log(result);

                            //console.log(BindData(result.Data, "null"));
                            that.Models = BindData(result.Data, "null");
                        }
                    },
                    error: function () {
                        alert("网络断开或服务异常");
                    }
                });

                function BindData(data, pid) {
                    var arry = [];
                    var currentData = Enumerable.From(data).Where("a => a.N_PARENT_PROJECT_ID ==" + pid).ToArray();
                    for (var i = 0; i < currentData.length; i++) {
                        arry.push(currentData[i]);
                        var currentData1 = BindData(data, currentData[i].N_PROJECT_ID);
                        for (var k = 0; k < currentData1.length; k++) {
                            arry.push(currentData1[k]);
                        }
                    }
                    return arry;
                }
            }
        });
    </script>
    </form>
</body>
</asp:Content>